<?php

declare(strict_types=1);

namespace App\Services;

use Hyperf\Di\Annotation\Inject;
use App\Exception\ApiException;
use App\Constants\ErrorCode;
use PhpOffice\PhpSpreadsheet\IOFactory;
use App\Model\User;

class ExcelService extends BaseService
{

    public function index()
    {
        $file = BASE_PATH.'/20231229.csv';
        $spreadsheet = IOFactory::load($file);
        $res = $spreadsheet->getActiveSheet()->toArray();
        // unset($res[0]);      //删除标题列
        foreach($res as $val)
        {
            if(!empty($val[1])){
                $nickname = trim($val[1]);
                $user = User::query()->where('nickname', $nickname)->orderBy('last_login_at', 'desc')->first();
                if($user)
                {
                    User::query()->where('id', $user['id'])->update(['star_currency'=>$user['star_currency']+1]);
                }else{
                    logger('command','crontab')->info('未导入编号======>'.$val[0]);
                }
            }else{
                logger('command','crontab')->info('未导入编号======>'.$val[0]);
            }
        }

        return [];
    }

    public function excelToStarCurrency()
    {
        $file = BASE_PATH.'/20240815-6.xlsx';
        $spreadsheet = IOFactory::load($file);
        $res = $spreadsheet->getActiveSheet()->toArray();
        foreach($res as $val)
        {
            if(!empty($val[0])){
                $nickname = trim($val[0]);
                $num = trim($val[1]);
                $user = User::query()->where('nickname', $nickname)->orderBy('last_login_at', 'desc')->first();
                if($user)
                {
                    User::query()->where('id', $user['id'])->update(['star_currency' => $user['star_currency']+$num]);
                }else{
                    logger('command','crontab')->info('6月未导入昵称======>'.$val[0]);
                }
            }else{
                logger('command','crontab')->info('6月未导入昵称======>'.$val[0]);
            }
        }

        return [];
    }
}